package com.autumn.platform.core.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

@Configuration
public class ResourceBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        String beanName = "messageSource";
        if (registry.containsBeanDefinition(beanName)) {
            return;
        }

        BeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);//单例
        beanDefinition.setBeanClassName(ReloadableResourceBundleMessageSource.class.getName());
        beanDefinition.setAutowireCandidate(true);
        registry.registerBeanDefinition(beanName, beanDefinition);
    }

}
